#include "lpr_infer_mnn_api.h"


std::shared_ptr<DetectRecPlate> CreateDrpApp()
{
    return std::make_shared<DetectRecPlateMnn>();
}


DetectRecPlate::DetectRecPlate(){

}

DetectRecPlate::~DetectRecPlate(){

}

void DetectRecPlate::blobFromImage_plate(cv::Mat& img,float mean_value,float std_value,float *blob)
{
    int channels = 3;
    int img_h = img.rows;
    int img_w = img.cols;
    int k = 0;
    for (size_t c = 0; c <3; c++) 
    {
        for (size_t  h = 0; h < img_h; h++) 
        {
            for (size_t w = 0; w < img_w; w++) 
            {
                    blob[k++] =
                    ((float)img.at<cv::Vec3b>(h, w)[c]/255.0-mean_value)/std_value;
            }
        }
    }
}

cv::Mat DetectRecPlate::get_split_merge(cv::Mat &img)   //双层车牌 分割 拼接
{
    cv::Rect  upper_rect_area = cv::Rect(0,0,img.cols,int(5.0/12*img.rows));
    cv::Rect  lower_rect_area = cv::Rect(0,int(1.0/3*img.rows),img.cols,img.rows-int(1.0/3*img.rows));
    cv::Mat img_upper = img(upper_rect_area);
    cv::Mat img_lower =img(lower_rect_area);
    cv::resize(img_upper,img_upper,img_lower.size());
    cv::Mat out(img_lower.rows,img_lower.cols+img_upper.cols, CV_8UC3, cv::Scalar(114, 114, 114));
    img_upper.copyTo(out(cv::Rect(0,0,img_upper.cols,img_upper.rows)));
    img_lower.copyTo(out(cv::Rect(img_upper.cols,0,img_lower.cols,img_lower.rows)));

    return out;
}

void DetectRecPlate::get_plate_result(cv::Mat &img,std::string &plate_number, float &plate_char_conf, std::string &plate_color, std::vector<float>& char_conf){

}

void DetectRecPlate::decode_outputs(float *prob,int output_size,  std::string &plate, float &plate_char_conf, std::vector<float>& char_conf)
{   
    std::string pre_str ="#";
    int char_length = output_size / 2;

    for (int i = 0; i<char_length; i++)
    {
        int index = int(prob[i]);
        if(index < int(plate_string_yingwen.size())){
            if (plate_string[index]!="#" && plate_string[index] != pre_str && plate_string[index]!="电动备案") {
                plate+=plate_string[index];
                char_conf.emplace_back(int(prob[char_length+i] * 100 + 0.5) / 100.0f);
            }
            pre_str = plate_string[index];
        }
    }

    plate_char_conf = std::accumulate(char_conf.begin(), char_conf.end(), 0.0) / char_conf.size();
}

std::string DetectRecPlate::decode_color_outputs(float *prob,int output_size) 
{
    return plate_color_string[int(prob[output_size-1])];
}

void DetectRecPlate::infer(cv::Mat &img, std::string &plate_number, float &plate_char_conf, std::string &plate_color, std::vector<float>& char_conf) 
{                   
 
}
